<?php

namespace App\service;


// 抽象活动算法类
use App\Models\GrGood;
use App\Models\GrUser;
use App\Models\OrderGood;
use App\Models\Orders;
use App\Models\Store;
use Illuminate\Support\Facades\DB;

abstract class  StrategyAbstract
{
    public abstract function pay($order_sn, $money, $token, $password);
}

//支付宝
class   FullReductionStrategy extends StrategyAbstract
{

    public function pay($order_sn, $money, $token, $password)
    {
        echo "我是支付宝";
//      $pay= new AlipayController();
//      $pay->web($money,$order_sn);
//      return redirect('http://www.day15.com/web/'.$order_sn.'/'.$money);

    }
}

//余额
class FullDiscountStrategy extends StrategyAbstract
{
    public function pay($order_sn, $money, $token, $password)
    {
        //获取user_id
        $jwtData = JwtService::decodeToken($token);
        $user_id = $jwtData->data;
        $data = GrUser::where('id', $user_id)->first()->toArray();
        //获取余额
        $balance = $data['balance'];
        //获取支付密码
        $payment_code = $data['payment_code'];
        if ($payment_code != md5($password)) {
            echo "密码错误";
        } else {
            //开始
            DB::beginTransaction();
            try {
                //判断余额
                $newMoney = $balance - $money;
                if ($newMoney < 0) {
                    return ReturnService::failedReturn('402', '余额不足');
                } else {

                    //扣除用户余额
                    $res = GrUser::where('id', $user_id)->update(['balance' => $newMoney]);
                    if ($res) {
                        //转给商户
                        $data = Orders::join('gr_order_goods', 'order.id', '=', 'gr_order_goods.order_id')
                            ->join('gr_goods', 'gr_order_goods.goods_id', '=', 'gr_goods.goods_id')->where('order.order_sn', $order_sn)->get()->toArray();
                        foreach ($data as $k => $v) {
                            $store = Store::where('id', $v['store_id'])->first();
                            //获取商家id
                            $res = GrUser::where('id', $store->user_id)
                                ->update(['balance' => GrUser::where('id', $store->user_id)->first()->balance + $money]);
                            if ($res) {
                                //修改订单状态
                                $result = Orders::where('order_sn', $order_sn)->update(['order_status' => 0, 'is_pay' => 1, 'pay_type' => 0]);
                            }
                        }
                    }
                }

                //提交
                Db::commit();
                if ($result) {
                    return ReturnService::successReturn('200', '支付成功', $money);
                } else {
                    return ReturnService::failedReturn('400', '支付失败');
                }
            } catch (\Exception $e) {
                //回滚事务
                Db::rollback();
            }
        }

    }

}

//策略工厂
class Pay
{
    private $strategy;

    public function __construct($strategy)
    {
        $this->strategy = $strategy;
    }

    public function getDiscount($order_sn, $money, $token, $password)
    {
        $this->strategy->pay($order_sn, $money, $token, $password);
    }
}

